package com.itc.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.poi.ExcelUtil;
import com.itc.common.log.annotation.Log;
import com.itc.common.log.enums.BusinessType;
import com.itc.common.security.annotation.InnerAuth;
import com.itc.common.security.annotation.RequiresPermissions;
import com.itc.system.api.domain.SysOperLog;
import com.itc.system.service.SysOperLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 * 
 * @author itc
 */
@RestController
@RequestMapping("/operlog")
@Api(value = "sysOperlog", tags = "操作日志管理模块")
public class SysOperlogController
{
    @Autowired
    private SysOperLogService operLogService;

//    @RequiresPermissions("system:operlog:list")
    @PostMapping("/page")
    @ApiOperation(value = "操作日志分页获取")
    public R<Page<SysOperLog>> getPage(@RequestBody SysOperLog operLog)
    {
        Page<SysOperLog> page = new Page<>();
        page.setOptimizeCountSql(true);
        page.setSize(operLog.getSize());
        page.setCurrent(operLog.getCurrent());
        Page<SysOperLog> list = operLogService.page(page,new LambdaQueryWrapper<>(operLog).orderByDesc(SysOperLog::getOperTime));
        return R.ok(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("system:operlog:export")
    @PostMapping("/export")
    @ApiOperation(value = "操作日志导出")
    public void export(HttpServletResponse response, @RequestBody SysOperLog operLog)
    {
        List<SysOperLog> list = operLogService.list(new LambdaQueryWrapper<>(operLog));
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @InnerAuth
    @PostMapping
    public R add(@RequestBody SysOperLog operLog)
    {
        return R.ok(operLogService.save(operLog));
    }
}
